/* ======================================================= */
/**
 * File   : MenuBar.java
 * Date   : 3 mars 2010 at 10:44:31
 */
/* ======================================================= */
package fr.isima.isipaint.gui;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.HashMap;

import javax.swing.AbstractAction;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;

import fr.isima.isipaint.controllers.ToolsController;
import fr.isima.isipaint.tools.NewAction;
import fr.isima.isipaint.tools.QuitAction;

/* ======================================================= */
/**
 * 
 * @author Daniel
 * 
 */
/* ======================================================= */
public class MenuBar extends JMenuBar {

	/** The serial number */
	private static final long serialVersionUID = 1L;

	/** Keys for the text dictonnary */
	private static final String FILE = "MENU_FILE";
	private static final String EDIT = "MENU_EDIT";
	private static final String TOOLS = "MENU_TOOLS";
	private static final String HELP = "MENU_HELP";

	private static final String NEW = "ITEM_NEW_FILE";
	private static final String QUIT = "ITEM_QUIT_FILE";
	private static final String UNDO = "ITEM_UNDO_EDIT";
	private static final String REDO = "ITEM_REDO_EDIT";
	private static final String CLEAR = "ITEM_CLEAR_TOOLS";
	private static final String ABOUT = "ITEM_ABOUT_HELP";

	/** The dictionnary of menu and item text */
	private HashMap<String, String> text = new HashMap<String, String>();

	/** The controller */
	private ToolsController toolsCtrl;

	/* ======================================================= */
	/**
	 * Default constructor
	 */
	/* ======================================================= */
	public MenuBar( ToolsController toolsCtrl) {
		this.toolsCtrl = toolsCtrl;

		text.put(TOOLS, "Tools");

		text.put(CLEAR, "clear");

		text.put(HELP, "Help");
		text.put(ABOUT, "about");

		text.put(NEW, "New");
		text.put(QUIT, "Quit");

		text.put(FILE, "File");

		text.put(UNDO, "Undo");
		text.put(REDO, "Redo");
		text.put(EDIT, "Edit");

		JMenu file = new JMenu(text.get(FILE));
		JMenu edit = new JMenu(text.get(EDIT));
		JMenu tools = new JMenu(text.get(TOOLS));
		JMenu help = new JMenu(text.get(HELP));

		add(file);
		add(edit);
		add(tools);
		add(help);

		JMenuItem tmp = new JMenuItem(text.get(NEW));
		tmp.setAction(new NewAction());
		tmp.setMnemonic((Integer) tmp.getAction().getValue(AbstractAction.MNEMONIC_KEY));
		tmp.setAccelerator(KeyStroke.getKeyStroke((Integer) tmp.getAction().getValue(AbstractAction.MNEMONIC_KEY), ActionEvent.CTRL_MASK));
		tmp.addActionListener(toolsCtrl);
		file.add(tmp);
		
		tmp = new JMenuItem(text.get(QUIT));
		tmp.setAction(new QuitAction());
		tmp.setMnemonic((Integer) tmp.getAction().getValue(AbstractAction.MNEMONIC_KEY));
		tmp.setAccelerator(KeyStroke.getKeyStroke((Integer) tmp.getAction().getValue(AbstractAction.MNEMONIC_KEY), ActionEvent.CTRL_MASK));
		tmp.addActionListener(toolsCtrl);
		file.add(tmp);

		edit.add(new JMenuItem(text.get(UNDO)));
		edit.add(new JMenuItem(text.get(REDO)));

		tools.add(new JMenuItem(text.get(CLEAR)));

		help.add(new JMenuItem(text.get(ABOUT)));

	}
}
